
angular.module('Demo')

.controller('Demo.HomeCtrl', ['$scope', '$state', '$timeout', 'imanDialog', 'imanUsbPort', function ($scope, $state, $timeout, imanDialog, imanUsbPort) {
    $scope.data = {};

    var barcodeMasters = [];
    for (var i = 1; i <= 20; i++) {
        barcodeMasters.push({
            MOLotNumber: 'P2012',
            ItemMasterCode: 'ST021',
            ItemMasterName: '网拍',
            Barcode: i.toString(),
            WorkName: '补土质检',
            //WorkLocationName: '补土质检站',
            UserName: '李红',
            DemandQty: 0
        });
    }
    $scope.data.gridOptions = {
        enableRefresh: true,
        enableCheck: true,
        enableOperate: true,
        operateDefs: [
            { name: 'op1', icon: u9ImHd.getFullPath('img/icon_list.png', 'Demo') },
            { name: 'op2', icon: u9ImHd.getFullPath('img/icon_item.png', 'Demo') }
        ],
        columnDefs: [ 
            { col: 'MOLotNumber', name: '批次' },
            { col: 'ItemMasterCode', name: '料号' },
            { col: 'ItemMasterName', name: '品名' },
            { col: 'Barcode', name: '芯片ID', cls: 'demo-column-green' },
            { col: 'WorkName', name: '作业' },
            //{ col: 'WorkLocationName', name: '工位' },
            { col: 'UserName', name: '特裁人' },
            { 
                col: 'DemandQty', 
                name: '要货数量',
                cls: 'demo-demand-column',
                tpls: '<im-number-input class="col im-record-column demo-demand" number="gridOptions.data[%d].DemandQty"></im-number-input>'
            }
        ],
        data: barcodeMasters
    };

    $scope.search = function (query) {

    };
    $scope.onOperate = function (operate, item) {
        if (operate === 'op1') {
            $state.go('Demo-detail');
        }
    };
    $scope.doRefresh = function (operate, item) {
        $scope.data.gridOptions.data.push({
            MOLotNumber: 'P2012',
            ItemMasterCode: 'ST021',
            ItemMasterName: '网拍',
            Barcode: '21',
            WorkName: '补土质检',
            WorkLocationName: '补土质检站',
            UserName: '李红'
        });
        $scope.$broadcast('scroll.refreshComplete');
    };

    $scope.showList = function () {
        imanDialog.showList({
            title: '请选择碰伤/划伤位置',
            list: [
                { id: 0, name: 'T头' },
                { id: 1, name: '5/7点' },
                { id: 2, name: '3/9点' },
                { id: 3, name: '2/10点' },
                { id: 4, name: '12点' },
                { id: 5, name: '内6点' },
                { id: 6, name: '内12点' },
                { id: 7, name: '内3/9' },
                { id: 8, name: '内5/7' },
                { id: 9, name: '拍柄' }
            ],
            valueField: 'id',
            displayField: 'name',
            value: 8
        }).then(function (select) {
            imanDialog.toastBottom(angular.toJson(select));
        });
    };
    $scope.confirm = function () {
        imanDialog.confirm('您的应用是最新版本', '版本检查').then(function (btn) {
            if (btn) {
                imanDialog.toastTop('测试弹出提示信息');
            } else {
                imanDialog.toastCenter('测试弹出提示信息');
            }
        });
    };
    $scope.alert = function () {
        imanDialog.alert('您的应用是最新版本', '版本检查').then(function () {
            imanDialog.toastBottom('测试弹出提示信息');
        });
    };
    $scope.prompt = function () {
        imanDialog.prompt('请输入文本', '备注').then(function (text) {
            imanDialog.toastBottom(text);
        });
    };
    $scope.showLoading = function () {
        imanDialog.showLoading();
        $timeout(function () {
            imanDialog.hideLoading();
        }, 6000);
    };

    $scope.$on('$ionicView.beforeEnter', function () {
        imanUsbPort.start();
    });
    $scope.$on('$ionicView.afterLeave', function () {
        imanUsbPort.stop();
    });
    $scope.$on('imanUsbPort.input', function (e, inputStr) {
        imanDialog.toastBottom(inputStr);
    });
}]);